package chpt11;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class URLPostTest {
	public static void main(String[] args) {
		String USER_AGENT = "Mozilla/5.0";
		BufferedReader in = null;
		try {
			URL url = new URL("http://localhost:8080/user/login");
			HttpURLConnection connection = 
					(HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestProperty("User-Agent", USER_AGENT);
			String postData = "username=abc&password=123456";
			OutputStream outputStream = connection.getOutputStream();
			outputStream.write(postData.getBytes("UTF-8"));
			outputStream.flush();
			outputStream.close();
			in = new BufferedReader(
					new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}